package com.intelligentpension.kotlinbluetooth.adapter

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.intelligentpension.kotlinbluetooth.R

/**
 * 版权：点赞 版权所有
 * author :赵航
 * @e-mail: zhaohangcn@gmail.com
 * time：2022/3/28
 * describe ：
 */
class BEAdapter(layoutResId: Int, data: MutableList<BluetoothDevice>) :
    BaseQuickAdapter<BluetoothDevice, BaseViewHolder>(layoutResId, data) {
    @SuppressLint("MissingPermission")
    override fun convert(holder: BaseViewHolder, item: BluetoothDevice) {
        var blAddress: String = item.address
        var state: Int = item.bondState

        var blNameText = item.name ?: "未知"

        holder.setText(R.id.name_text, blNameText+"=="+deviceType(item));

        var stateText = if (state === 10) {
            "未配对"
        } else {
            "配对"
        }

        holder.setText(R.id.address_text, String.format("%s (%s)", blAddress, stateText))
    }

    @SuppressLint("MissingPermission")
    fun deviceType(dev :BluetoothDevice?) :String{
        return when(dev?.type){
            BluetoothDevice.DEVICE_TYPE_LE ->{
                "类型:低功耗"
            }
            BluetoothDevice.DEVICE_TYPE_CLASSIC->{
                "类型:经典型"
            }
            BluetoothDevice.DEVICE_TYPE_DUAL->{
                "类型:双支持(及支持低功耗,又支持经典蓝牙)"
            }
            else ->{
                "类型:未知的"
            }
        }
    }
}